/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package GamePlay;

import FrameWork.Audio.RSAudio;
import FrameWork.IPlay;
import FrameWork.Point;
import FrameWork.RSFont;
import FrameWork.Rectangle;
import java.io.IOException;
import javax.microedition.lcdui.Image;
import javax.microedition.lcdui.game.Sprite;

/**
 *
 * @author DANTRUONG
 */
public class State_Option extends Virtual_Menu{
    Sprite m_Dialog;
    Sprite m_Ball;
    Sprite m_Ball1;
    int m_IDChoosed;
    int m_Status;
    int m_Volume;
    static int m_OldVolume = 5;
    Rectangle[] m_RectButton = new Rectangle[3];
    Rectangle[] m_RectVolume = new Rectangle[5];
    
    public State_Option(IPlay _Play){
        super(_Play);
        m_IDState = Global.STATE_OPTION ;
    }
    
    public void Init (){
        super.Init();
        try {
            m_Dialog = new Sprite (Image.createImage("/Image/dialog.png"),
                            204, 235);
            m_Ball = new Sprite (Image.createImage("/Image/ball.png"),
                            60, 60);
            m_Ball1 = new Sprite (Image.createImage("/Image/ball1.png"),
                            30, 30);
        } catch (IOException ex) {
            ex.printStackTrace();
        }
        
        m_IDChoosed = 0;
        m_Status = 0;
        m_Volume = m_OldVolume;
        m_RectButton[0] = new Rectangle(28, 105, 60, 60);
        m_RectButton[1] = new Rectangle(152, 105, 60, 60);
        m_RectButton[2] = new Rectangle(90, 180, 60, 60);
        for(int i =0; i<5; i++){
            m_RectVolume[i] = new Rectangle(34 + 35*i, 145, 30, 30);
        }
    }    
     public void PointerPressed(int x, int y){
        if(m_Status ==0){
            for(int i = 0; i<2; i++){
                if(m_RectButton[i].Intersects(new Point(x,y))){
                    m_IDChoosed = i;
                    RSAudio.getRS().PlayBubble();
                    if(m_IDChoosed == 0){
                    RSAudio.getRS().setEnable(true);
                    m_Status = 1;
                    }
                    if(m_IDChoosed == 1){
                        RSAudio.getRS().StopBack();
                        RSAudio.getRS().setEnable(false);
                    }
                }
            }
        }else if(m_Status == 1){
            for(int i = 0; i<5; i++){
                if(m_RectVolume[i].Intersects(new Point(x,y))){
                    m_Volume = i+1;
                    RSAudio.getRS().PlayBubble();
                    RSAudio.getRS().setVolume(m_Volume*20);
                }
            }
        }
        if(m_RectButton[2].Intersects(new Point(x,y))){
            m_IDChoosed = 2;
            RSAudio.getRS().PlayBubble();
            m_OldVolume = m_Volume;
            m_Play.setNextState(
                      new State_Menu(m_Play));
        }
    }
    public void KeyPressed (int _Keycode){
        if(m_Status ==0){
            if (_Keycode == -3 || _Keycode == 52 || _Keycode == -4 || _Keycode == 54){
                RSAudio.getRS().PlayBubble();
                if(m_IDChoosed == 0)
                    m_IDChoosed = 1;
                else if(m_IDChoosed == 1)
                    m_IDChoosed = 0;
            }
            
            if (_Keycode == -1 || _Keycode == 50){
                RSAudio.getRS().PlayBubble();
                if(m_IDChoosed == 2)
                    m_IDChoosed = 0;
            }
            
            if (_Keycode == -5 || _Keycode == 53){
                RSAudio.getRS().PlayBubble();
                if(m_IDChoosed == 0){
                    RSAudio.getRS().setEnable(true);
                    m_Status = 1;
                }
                if(m_IDChoosed == 1){
                    RSAudio.getRS().StopBack();
                    RSAudio.getRS().setEnable(false);
                }
            }
        }
        else if(m_Status == 1){
            if (_Keycode == -3 || _Keycode == 52){
                RSAudio.getRS().PlayBubble();
                m_Volume -= 1;
                if(m_Volume<1)
                    m_Volume = 1;
                RSAudio.getRS().setVolume(m_Volume*20);
            }
            
            if (_Keycode == -4 || _Keycode == 54){
                RSAudio.getRS().PlayBubble();
                m_Volume += 1;
                if(m_Volume>5)
                    m_Volume = 5;
                RSAudio.getRS().setVolume(m_Volume*20);
            }
        }
        
        if (_Keycode == -2 || _Keycode == 56){
            RSAudio.getRS().PlayBubble(); 
            m_IDChoosed = 2;       
        }

        if (_Keycode == -5 || _Keycode == 53){
            RSAudio.getRS().PlayBubble();
            if(m_IDChoosed == 2){
                m_OldVolume = m_Volume;
                m_Play.setNextState(
                      new State_Menu(m_Play));
            }
        }
    }
    
    public void Render (){
       super.Render();
        
       m_Dialog.setFrame(0);
       m_Dialog.setPosition(18, 15);
       m_Dialog.paint(m_Graphics);
        
       RSFont.getRS().getFont(Global.FONT_SMALL).DrawString(
                        "BACK", 107, 205, m_Graphics);
       if(m_IDChoosed == 2){
           m_Ball.setFrame(1);
           m_Ball.setPosition(90, 180);
           m_Ball.paint(m_Graphics);
       }
       else{
           m_Ball.setFrame(0);
           m_Ball.setPosition(90, 180);
           m_Ball.paint(m_Graphics);
       }
       
       switch(m_Status){
           case 0:
               RSFont.getRS().getFont(Global.FONT_SMALL).DrawString(
                        "Enable", 100, 130, m_Graphics);
               RSFont.getRS().getFont(Global.FONT_SMALL).DrawString(
                        "Sound?", 100, 140, m_Graphics);
               RSFont.getRS().getFont(Global.FONT_SMALL).DrawString(
                        "YES", 48, 130, m_Graphics);
               RSFont.getRS().getFont(Global.FONT_SMALL).DrawString(
                        "NO", 175, 130, m_Graphics);
               if(m_IDChoosed == 0){
                   m_Ball.setFrame(1);
                   m_Ball.setPosition(28, 105);
                   m_Ball.paint(m_Graphics);
               }
               else{
                   m_Ball.setFrame(0);
                   m_Ball.setPosition(28, 105);
                   m_Ball.paint(m_Graphics);
               }
               if(m_IDChoosed == 1){
                   m_Ball.setFrame(1);
                   m_Ball.setPosition(152, 105);
                   m_Ball.paint(m_Graphics);
               }
               else{
                   m_Ball.setFrame(0);
                   m_Ball.setPosition(152, 105);
                   m_Ball.paint(m_Graphics);
               }
               break;
           case 1:
               RSFont.getRS().getFont(Global.FONT_SMALL).DrawString(
                        "Volume", 99, 130, m_Graphics);
               for(int i = m_Volume + 1; i<=5; i++){
                   m_Ball1.setFrame(0);
                   m_Ball1.setPosition(34 + 35*(i-1), 145);
                   m_Ball1.paint(m_Graphics);
               }
               for(int i = 1; i<= m_Volume; i++){
                   m_Ball1.setFrame(1);
                   m_Ball1.setPosition(34 + 35*(i-1), 145);
                   m_Ball1.paint(m_Graphics);
               }
               break;             
       } 
    }
    public void Destroy(){
        m_Dialog = null;
        m_Ball = null;
        m_Ball1 = null;
    }
            
}
